<?php

ini_set('display_errors', 1);
error_reporting(E_ALL &~ E_NOTICE);

set_time_limit(0);

header("Content-type:text/html; charset=utf-8");
header("Expires: Mon, 26 Jul 1970 05:00:00 GMT"); 
header("Last-Modified: ".gmdate('D, d M Y H:i:s')." GMT");
header("Cache-control: no-cache, must-revalidate");
header("Pragma: no-cache");


unset($GLOBALS);

$_ENV = array();

define('__PATH__',dirname(__FILE__));
define('CACHE_PATH',__PATH__.'/source/cache/');
define('ATTACH_PATH',__PATH__.'/attach/');
define('IN_SYSTEM',1);
define('POST',strtolower($_SERVER['REQUEST_METHOD']) === 'post');
define('AJAX',(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') OR $_GET['inajax'] == '1');
define('REFER',$_SERVER['HTTP_REFERER']);
define('AUTH_KEY','this is custom auth secret key'.$_SERVER['HTTP_USER_AGENT']);

date_default_timezone_set('PRC');

$_ENV['time'] = time();

/**
 * 加载全局配置文件
 */
$config = array();
@include __PATH__.'/source/config.global.php';
$_ENV['config'] =  $config;

/**
 * 加载所需文件
 */
require __PATH__.'/source/function/function_core.php';
require __PATH__.'/source/function/function_admincp.php';
define('FORMHASH',formhash());

/**
 * 配置系统
 */
spl_autoload_register('import');

/**
 * 连接数据库
 */
$_ENV['db'] = new class_db($_ENV['config']['db']['host'],$_ENV['config']['db']['user'],$_ENV['config']['db']['pass'],$_ENV['config']['db']['name']);

/**
 * 加载系统设置
 */
$config = array();
if (class_cache::has('config')) {
    $config = class_cache::read('config');
} else {
    $rowset = $_ENV['db']->fetchAll("SELECT * FROM `setting`");
    foreach ((array)$rowset as $row) {
        $config[$row['k']] = $row['v'];
    }
    class_cache::write('config',$config);
}
$_ENV['config'] = array_merge($_ENV['config'],$config);
unset($config);

/**
 * 请假一年一清零
 */
$currYear = date("Y");   				                      								//服务器端当前年份
$preYear  = $_ENV['db']->fetchCol("SELECT v FROM `setting` where `k`='vacation_round' ");  //上次清零年份
if($currYear!=$preYear)
{  //上次清零年份跟当前年份不一致
	$_ENV['db']->runSql("update `admincp_member_detail` SET `total_vacation` = '0' ");
	$_ENV['db']->runSql("update `setting` SET `v` = '{$currYear}' where `k`='vacation_round' ");
}

/**
 * 请求过滤
 */
if(!get_magic_quotes_gpc()) {
    $_GET = caddslashes($_GET);
    $_POST = caddslashes($_POST);
    $_REQUEST = array_merge($_GET,$_POST);
    $_COOKIE = caddslashes($_COOKIE);
}

/**
 * 请求转发
 */
$app = empty($_REQUEST['app']) ? 'portal' : $_REQUEST['app'];
$mod = empty($_REQUEST['mod']) ? 'main' : $_REQUEST['mod'];

define('APP',$app);
define('MOD',$mod);
$_ENV['app'] = APP;
$_ENV['mod'] = MOD;

$entity=$app.'Entity';
$app.='ControlPage';
$mod.='Module';

if (!file_exists("./source/app/{$app}.php")) $app = 'portalControlPage';

$app = new ReflectionClass($app);
call_user_func(array($app->newInstanceArgs(array()),$mod));